﻿using System;
using System.Linq.Expressions;
using System.Web.UI;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.ContentDL
{
	public partial class ComDetail : MemberBasePage
	{
		protected string Con1 { get; set; }
		protected string Con2 { get; set; }
		protected string DateP { get; set; }
		protected string CountR { get; set; }
		protected string Img { get; set; }

		private int NewsID
		{
			get;
			set;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			ImgData();
			if (!Page.IsPostBack)
			{
				int id = 0;
				if (int.TryParse(Request["id"], out id))
				{
					this.NewsID = id;
				}
				BindData();

				news_subtitle.Visible = string.IsNullOrWhiteSpace(Request.QueryString["nc"]);
			}
		}

		protected void BindData()
		{
			Expression<Func<TB_News, bool>> f;
			if (this.NewsID == 0)
			{
				f = (p => p.Type == this.SystemCode && !p.IsEn);
			}
			else
			{
				f = (p => p.ID == this.NewsID && p.Type == this.SystemCode && !p.IsEn);
			}

			var resultQ = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_News>(f);

			if (resultQ != null)
			{
				if (resultQ.IsPrivate && !IsLogin)// 数据是否登入后可见，是否登入
				{
					RedirectWithMessage(MemberLoginPage, "当前页面需要登录后才可访问,请登录!");
				}
				else
				{
					this.linkAttarchment.HRef = resultQ.AttachmentsUrl;
					bool ctrlvisible = String.IsNullOrWhiteSpace(resultQ.AttachmentsUrl);
					this.linkAttarchment.Visible = !ctrlvisible;
					this.dashed_line.Visible = !ctrlvisible;
					this.Con1 = resultQ.Title.ToString();
					this.DateP = resultQ.CreateDate.ToString("yyyy-MM-dd");
					this.CountR = resultQ.ReadCount.ToString();
					if (!string.IsNullOrWhiteSpace(resultQ.Content))
					{
						this.Con2 = resultQ.Content.ToString();
					}
					else
					{
						this.Con2 = resultQ.Summary.ToString();
					}

					resultQ.ReadCount = resultQ.ReadCount + 1;
					DAL.DALHelper.Instance.Update<TB_News>(resultQ);
					DAL.DALHelper.Instance.SaveChanges();
				}
			}
		}

		protected void ImgData()
		{
			string type;

			if (string.IsNullOrWhiteSpace(this.SystemCode))
			{
				// TODO 默认图片
				type = "";
			}
			else
			{
				type = this.SystemCode;
			}
			Expression<Func<TB_SystemConfig, bool>> f = (p => p.Code == "Img" && p.Type == type);
			var resultQ = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_SystemConfig>(f);
			if (resultQ != null)
			{
				this.Img = resultQ.Value.ToString();
			}
		}
	}
}